import { client } from '../../request/index.js'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'

Page({
  data: {
    ip: getApp().globalData.ip,
    imgIp: getApp().globalData.imgIp,
    arr: null,
    dt_aCoin: '',
    id: '',
    donationPanelHidden: true,
    user_name: '',
    inputV: '',
    guide: false,
  },

  async getACoin() {
    const mapData = await client({
      url: '/account/detail',
    })
    console.log('用户信息:', mapData)
    this.setData({
      dt_aCoin: mapData.data.data.aCoin,
    })
  },

  donation() {
    this.setData({
      donationPanelHidden: false,
      inputV: '',
    })
  },

  viewImg(e) {
    console.log(e)
    let imgUrl = e.currentTarget.dataset.url
    wx.previewImage({
      urls: [imgUrl],
      current: '',
      success: function (res) {},
    })
  },

  async donation_comfirm(e) {
    let that = this
    this.setData({
      donationPanelHidden: true,
    })
    let dtUrl = '/donate/donate'
    const pid = that.data.id
    let dtRequestUrl =
      dtUrl + '?donate=' + e.detail.value.donate + '&poorManId=' + pid
    if (
      e.detail.value.donate >
      that.data.arr.needMoney - that.data.arr.currentMoney
    ) {
      Toast('您的爱心溢出来惹，不需要这么多哟')
    } else if (e.detail.value.donate <= 0) {
      Toast('抱歉，请输入大于零的数额')
    } else {
      const getData = await client({
        url: dtRequestUrl,
        method: 'POST',
        dataType: 'json',
      })
      console.log('助力返回：', getData)
      if (getData.data.message == '捐赠成功') {
        that.onLoad({
          id: that.data.id,
        })
        Toast.success('助力成功')
      } else if (getData.data.message == '金额不足') {
        Toast('爱币不足,助力失败')
      }
    }
  },

  donation_cancel: function () {
    this.setData({
      donationPanelHidden: true,
    })
  },

  async getPoorInfo(id) {
    const mapData = await client({
      url: `${'/poor-man-info/public/findlistbyid'}/${id}`,
    })
    console.log('单贫困户信息：', mapData)
    this.setData({
      user_name: mapData.data.data.username,
      id: mapData.data.data.id,
      arr: mapData.data.data,
    })
  },
  // 取消引导
  guideDt1_cancel() {
    wx.setStorage({
      key: 'dtXqGuideOpen',
      data: false,
    })
    this.setData({
      guide: false,
    })
  },

  onLoad: function (options) {
    this.getPoorInfo(options.id)
    this.getACoin()
  },

  onShow() {
    // 判断是否显示过引导
    let dtXqGuideOpen = wx.getStorageSync('dtXqGuideOpen')
    if (dtXqGuideOpen === true) {
      this.setData({
        guide: true,
      })
    } else {
      this.setData({
        guide: false,
      })
    }
  },
})
